import React, { Component, createRef } from 'react';
// 非受控组件
class ProductView extends Component {
  // userRef = createRef();// React.createRef() 
  constructor() {
    super()
    this.userRef = createRef();
    this.submitHandler = this.submitHandler.bind(this)
  }
  submitHandler() {
    console.log(this.userRef.current.value)
    let username = this.userRef.current.value
    if (/^\d*$/.test(username)) {
      console.log('输入数字')
    } else {
      console.log('没有输入数字')
      this.userRef.current.focus();
      this.userRef.current.select();
    }
  }
  render() {
    return (
      <div>
        <div>
          UserName:<input ref={this.userRef} type='text' defaultValue="wqw" />
        </div>
        <div>
          <button type="button" onClick={this.submitHandler}>submit</button>
        </div>
      </div>
    );
  }
}

export default ProductView;
